package com.itheima.bos.service.base.Impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.base.CourierRepository;
import com.itheima.bos.domain.base.Courier;
import com.itheima.bos.service.base.CourierService;
@Service
@Transactional
public class CourierServiceImpl implements CourierService {
	
	@Autowired
	private CourierRepository courierRepository;
	@Override
	public void save(Courier courier) {
		courierRepository.save(courier);
	}
	
	@Override
	@RequiresPermissions("courier:add")
	public Page<Courier> findPageData(Specification<Courier> specification, Pageable pageable) {
		
		return courierRepository.findAll(specification, pageable);
	}

	@Override
	public void delBatch(String[] idsArray) {
		
		for (String idStr : idsArray) {
			Integer id = Integer.parseInt(idStr);
			courierRepository.updateBatch(id);
		}
	}

	
	@Override
	public List<Courier> findNoAssociation() {
		Specification<Courier> specification = new Specification<Courier>(){

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return p;
			}
			//查询语句
			//select tc.*,tf.*from
			//t_courier tc left join t_fixedarea_courier fc on tc.c_id = fc.c_courier_id 
			//left join t_fixed_area tf on fc.c_fixed_area_id = tf.c_id
			//where tf.c_id is null
		};
		return courierRepository.findAll(specification);
	}

}
